package cc.atliberty.oa.controller;

import cc.atliberty.oa.service.EmployeeService;
import cc.atliberty.oa.utils.ResultObj;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/employee/deleteEmployee")
public class DeleteEmployee extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        String identity = request.getParameter("identity");
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        EmployeeService employeeService = context.getBean("EmployeeService", EmployeeService.class);
        
        ObjectMapper mapper = new ObjectMapper();
        String jsonStr = null;
        try {
            employeeService.deleteEmployee(identity);
            jsonStr = mapper.writeValueAsString(ResultObj.DELETE_SUCCESS);
        } catch (Exception e){
            jsonStr = mapper.writeValueAsString(ResultObj.DELETE_ERROR);
        }
        response.getWriter().print(jsonStr);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
    }
}
